﻿using System;
using System.Threading.Tasks;
using System.Linq;

namespace Ecobee.Net.Demo
{
	public class ThermostatDetailsCommand : CommandBase
	{
		public ThermostatDetailsCommand()
			: base("4", "Show Thermostat Detail")
		{
		}

		protected async override Task<bool> OnExcute(Api api)
		{
			bool returnValue = false;

			// ***
			// *** Use the ThermostatSummary to get a list of thermostats
			// ***
			ThermostatSummary thermostatSummary = new ThermostatSummary(api);

			// ***
			// *** Get the list
			// ***
			if (await thermostatSummary.Request())
			{
				// ***
				// *** Call the ecobee API to get the list of thermostats. This call
				// *** returns summary data only (very limited). It is used to check
				// *** if anything has changed on a thermostat recently or to get the 
				// *** initial list.
				// ***
				Thermostats thermostats = new Thermostats(api);

				// ***
				// *** Select the first thermostat
				// ***
				SelectionItems selectionItems = new SelectionItems()
				{
					IncludeAlerts = true,
					IncludeEvents = true,
					IncludeRuntime = true,
					IncludeSettings = true,
					IncludeProgram = true
				};

				// ***
				// *** Load the details for the first thermostat only
				// ***
				if (await thermostats.Request(thermostatSummary.SummaryRevisionList.Take(1), selectionItems))
				{
					Console.WriteLine("Identifier - {0}", thermostats[0].Identifier);
					Console.WriteLine("Name - {0}", thermostats[0].Name);
					Console.WriteLine("Current Temperature - {0}", thermostats[0].Runtime.ActualTemperature.RealTemperature.ToString("#0.0"));
					Console.WriteLine("There are {0} alert(s).", thermostats[0].Alerts.Count());

					// ***
					// *** Current Climate
					// ***
					if (thermostats[0].Events.Count() == 0)
					{
						var qry = from tbl in thermostats[0].Program.Climates
								  where tbl.ClimateReference == thermostats[0].Program.CurrentClimateRef
								  select tbl;

						if (qry.Count() == 1)
						{
							Console.WriteLine("Current Climate: {0}", qry.Single().Name);
						}
					}
					else
					{
						var qry = from tbl in thermostats[0].Events
								  where tbl.Running
								  orderby tbl.StartDateTime ascending
								  select tbl;

						if (qry.Count() > 0)
						{
							Event thermostatEvent = qry.First();

							if (thermostatEvent.EndDate == ApiDate.InfiniteDate)
							{
								Console.WriteLine("The System is in {0} Indefinitely", thermostatEvent.Type.ToString());
							}
							else if (thermostatEvent.EndDate == DateTime.Now.Date)
							{
								if (thermostatEvent.Type == EventType.Vacation)
								{
									Console.WriteLine("The System is in {0} Mode until {1}", thermostatEvent.Type.ToString(), thermostatEvent.EndDateTime.ToString("t"));
								}
								else
								{
									Console.WriteLine("The System is in {0} until {1}", thermostatEvent.Type.ToString(), thermostatEvent.EndDateTime.ToString("t"));
								}
							}
							else
							{
								Console.WriteLine("The System is in {0} until {1} on {2}", thermostatEvent.Type.ToString(), thermostatEvent.EndDateTime.ToString("t"), thermostatEvent.EndDateTime.ToString("M"));
							}
						}
					}
				}

				returnValue = true;
			}

			return returnValue;
		}
	}
}
